package com.woniuxy.aop;

/**
 * @author 佘赛飞
 * @create 2021 -11 -16 3:28 PM
 */

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 切面类，在切面类中，定义通知(其实也可以定义切入点)
 */
public class MyAspect {
    /**
     * 前置通知
     *
     * @param joinPoint
     */
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
        System.out.println("MyAspect.beforeAdvice:前置通知，模拟检查有没有权限……");
        System.out.print("目标类是：" + joinPoint.getTarget());
        System.out.println(",被增强的目标类的对应的方法是：" + joinPoint.getSignature().getName());
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
    }

    /**
     * 后置通知
     *
     * @param joinPoint
     */
    public void afterReturnningAdvice(JoinPoint joinPoint) {
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
        System.out.print("MyAspect.afterAdvice:后置通知，模拟记录日志……");
        System.out.println(",被增强的目标类的对应的方法是：" + joinPoint.getSignature().getName());
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
    }

    /**
     * 异常通知
     *
     * @param joinPoint
     * @param e         传入的异常名称
     */
    public void throwingAdvice(JoinPoint joinPoint, Throwable e) {
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
        System.out.println("异常通知：出了什么异常" + e.getMessage());
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
    }

    /**
     * 最终通知
     */
    public void afterAdvice() {
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
        System.out.println("无论目标类成功与否，我都要执行，用来释放资源……");
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
    }

    /**
     * 环绕通知
     *
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
        System.out.println("我是个单纯的环绕通知，我啥都干不了……");
        Object[] args = proceedingJoinPoint.getArgs();
        for (Object arg : args) {
            System.out.println("入参" + arg);
        }
        args[0] = 5;//将目标类的第一个参数修改为null
        //相当于之前手写的动态代理方法 method.invoke  表示我们的程序目标方法可以继续执行
        Object proceed = proceedingJoinPoint.proceed(args);


        System.out.println("这里是环绕结束的点");
        System.out.println("……………………………………………………………………………………………………………………………………………………………………………………");
        proceed = 100000;
        return proceed;
    }
}